class Observer {
    constructor(data) {
        this.walk(data)
    }
    walk (data) {
        // 判断data为对象
        if(!data || typeof data !== 'object') {
            return
        }
        Object.keys(data).forEach(key => {
            this.defineReactive(data, key, data[key])
        })
    }
    defineReactive (data, key, val) {
        const that = this
        // 收集依赖 并发送通知
        let dep = new Dep()
        // 针对对象属性
        this.walk(val)
        Object.defineProperty(data, key, {
            enumerable: true,
            configurable: true,
            get() {
                Dep.target && dep.addSubs(Dep.target)
                return val
            },
            set(newValue) {
                if (newValue === val) {
                    return 
                }
                val = newValue
                that.walk(newValue)
                dep.notify()
            }
        })
    }
}